home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / util / gnu / WinGnuPlot.lha / WinGnuPlot / Source / OutlineFont.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-13  |  5.8 KB  |  282 lines

  1. /* this defines some routines for handling of Outline-Fonts */
  2.  
  3. #include "struct.c"
  4. #include "OutlineFont.h"
  5.  
  6. #define CW 80
  7. #define CH 35
  8.  
  9. static int   cwidth, cheight;
  10. static ULONG XYdpi, PointSize;
  11.  
  12. struct Glyph
  13. {
  14.  int    top, left, width, height;
  15.  int    basex, basey;
  16.  int    bmsize, bpr;
  17.  UBYTE *bitmap;
  18. };
  19.  
  20. static struct Glyph *HGlyphs[256], *VGlyphs[256];
  21.  
  22.        struct Library     *BulletBase   = NULL;
  23. static struct GlyphEngine *BulletEngine = NULL;
  24.  
  25.  
  26. static BOOL IsOutlineFont(char *fontname)
  27. {
  28.  BPTR                       FontsDir;
  29.  BOOL                       h = FALSE;
  30.  struct FontContentsHeader *fch;
  31.  
  32.  if (FontsDir = Lock("FONTS:", ACCESS_READ))
  33.  
  34.   {
  35.    if (fch = NewFontContents(FontsDir, fontname))
  36.     {
  37.      if (fch->fch_FileID == OFCH_ID)
  38.       h = TRUE;
  39.      DisposeFontContents(fch);
  40.     }
  41.    UnLock(FontsDir);
  42.   }
  43.  
  44.  return h;
  45. }
  46.  
  47. static struct GlyphEngine *GetEngine(char *fontname, struct TagItem **OTags)
  48. {
  49.  BPTR                f;
  50.  int                 l;
  51.  char                enginename[80];
  52.  struct TagItem     *Tags, *ti, *tstate;
  53.  struct GlyphEngine *Engine = NULL;
  54.  
  55.  if (f = Open(fontname, MODE_OLDFILE))
  56.   {
  57.    Seek(f, 0, OFFSET_END);
  58.    l = Seek(f, 0, OFFSET_BEGINNING);
  59.    if (Tags = malloc(l))
  60.     {
  61.      Read(f, Tags, l);
  62.  
  63.      tstate = Tags;
  64.      while (ti = NextTagItem(&tstate))
  65.       if (ti->ti_Tag & OT_Indirect)
  66.        ti->ti_Data += (ULONG)Tags;
  67.      
  68.      if ((ti = FindTagItem(OT_FileIdent, Tags)) && (ti->ti_Data == l))
  69.       {
  70.        if (ti = FindTagItem(OT_Engine, Tags))
  71.     {
  72.      strcpy(enginename, (char *)(ti->ti_Data));
  73.      strcat(enginename, ".library");
  74.      if (BulletBase = OpenLibrary(enginename, 0))
  75.       {
  76.        Engine = OpenEngine();
  77.        *OTags = Tags;
  78.       }
  79.     }
  80.       }
  81.     }
  82.    Close(f);
  83.   }
  84.  
  85.  return Engine;
  86. }
  87.  
  88. static struct Glyph *GetGlyph(char ch, BOOL Vertical)
  89. {
  90.  struct GlyphMap  *GlyphMap;
  91.  struct Glyph    **glyphs;
  92.  
  93.  glyphs = (Vertical) ? VGlyphs : HGlyphs;
  94.  if (!glyphs[ch])
  95.   {
  96.    glyphs[ch] = malloc(sizeof(struct Glyph));
  97.    SetInfo(BulletEngine,
  98.        OT_GlyphCode, ch,
  99.        OT_RotateSin,   (Vertical) ? 0x00010000 : 0x00000000,
  100.        OT_RotateCos,   (Vertical) ? 0x00000000 : 0x00010000,
  101.        OT_PointHeight, PointSize,
  102.        OT_DeviceDPI,   XYdpi,
  103.        OT_DotSize,     0x00640064,
  104.        TAG_END);
  105.    if (ObtainInfo(BulletEngine,
  106.           OT_GlyphMap, &GlyphMap,
  107.           TAG_END) == OTERR_Success)
  108.     {
  109.      glyphs[ch]->top = GlyphMap->glm_BlackTop;
  110.      glyphs[ch]->left = GlyphMap->glm_BlackLeft;
  111.      glyphs[ch]->height = GlyphMap->glm_BlackHeight+1;
  112.      glyphs[ch]->width = GlyphMap->glm_BlackWidth+1;
  113.      glyphs[ch]->basey = GlyphMap->glm_Y0-GlyphMap->glm_BlackTop;
  114.      glyphs[ch]->basex = GlyphMap->glm_X0-GlyphMap->glm_BlackLeft;
  115.      glyphs[ch]->bpr = GlyphMap->glm_BMModulo;
  116.      glyphs[ch]->bmsize = GlyphMap->glm_BMModulo*GlyphMap->glm_BMRows;
  117.      if (glyphs[ch]->bitmap = AllocMem(glyphs[ch]->bmsize, MEMF_CHIP))
  118.       CopyMemQuick(GlyphMap->glm_BitMap, glyphs[ch]->bitmap, glyphs[ch]->bmsize);
  119.      ReleaseInfo(BulletEngine,
  120.          OT_GlyphMap, GlyphMap,
  121.          TAG_END);
  122.     }
  123.    else
  124.     {
  125.      glyphs[ch]->top = 0;
  126.      glyphs[ch]->left = 0;
  127.      glyphs[ch]->height = cheight;
  128.      glyphs[ch]->width = cwidth;
  129.      glyphs[ch]->basey = 0;
  130.      glyphs[ch]->basex = 0;
  131.      glyphs[ch]->bpr = (cwidth/32 + 1) * 4;
  132.      glyphs[ch]->bmsize = glyphs[ch]->bpr * cheight;
  133.      glyphs[ch]->bitmap = AllocMem(glyphs[ch]->bmsize, MEMF_CHIP|MEMF_CLEAR);
  134.     }
  135.   }
  136.  
  137.  
  138.  return glyphs[ch];
  139. }
  140.  
  141. static void FreeGlyph(struct Glyph *glyph)
  142. {
  143.  FreeMem(glyph->bitmap, glyph->bmsize);
  144. }
  145.  
  146. static void FreeGlyphs(void)
  147. {
  148.  int ch;
  149.  
  150.  for(ch=0; ch <= 255; ch++)
  151.   {
  152.    if (HGlyphs[ch])
  153.     {
  154.      FreeGlyph(HGlyphs[ch]);
  155.      free(HGlyphs[ch]);
  156.      HGlyphs[ch] = NULL;
  157.     }
  158.    if (VGlyphs[ch])
  159.     {
  160.      FreeGlyph(VGlyphs[ch]);
  161.      free(VGlyphs[ch]);
  162.      VGlyphs[ch] = NULL;
  163.     }
  164.   }
  165. }
  166.  
  167. BOOL OpenOutlineFont(char *font)
  168. {
  169.  char            fontname[80], otagname[80];
  170.  struct TagItem *OTags;
  171.  int             ch;
  172.  
  173.  strcpy(fontname, font); strcat(fontname, ".font");
  174.  if (IsOutlineFont(fontname))
  175.   {
  176.    strcpy(otagname, "FONTS:"); strcat(otagname, font); strcat(otagname, ".otag");
  177.    if (BulletEngine = GetEngine(otagname, &OTags))
  178.     {
  179.      strcpy(fontname, "FONTS:"); strcat(fontname, font); strcat(fontname, ".font");
  180.      SetInfo(BulletEngine,
  181.          OT_OTagPath, fontname,
  182.          OT_OTagList, OTags,
  183.          TAG_END);
  184.  
  185.      for(ch=0; ch <= 255; ch++)
  186.       HGlyphs[ch] = VGlyphs[ch] = NULL;
  187.  
  188.      return TRUE;
  189.     }
  190.   }
  191.  return FALSE;
  192. }
  193.  
  194. void CloseOutlineFont(void)
  195. {
  196.  FreeGlyphs();
  197.  if (BulletEngine) CloseEngine(BulletEngine);
  198.  if (BulletBase)   CloseLibrary(BulletBase);
  199. }
  200.  
  201. void PrintString(struct RastPort *rp, int x0, int y0, char *s, BOOL Vertical)
  202. {
  203.  char         *ch;
  204.  int           x, y;
  205.  struct Glyph *glyph;
  206.  
  207.  x = x0; y = y0;
  208.  for(ch = s; *ch; ch++)
  209.   {
  210.    glyph = GetGlyph(*ch, Vertical);
  211.    BltTemplate(glyph->bitmap+glyph->top*glyph->bpr,
  212.            glyph->left, glyph->bpr,
  213.            rp,
  214.            x-glyph->basex, 
  215.            y-glyph->basey,
  216.            glyph->width, glyph->height);
  217.    WaitBlit();
  218.    if (Vertical)
  219.     y -= glyph->height;
  220.    else
  221.     x += glyph->width;
  222.   }
  223. }
  224.  
  225. void SetFontSize(int w, int h)
  226. {
  227.  static int last_x = 0, last_y = 0, last_p = 0;
  228.  int x, y, p;
  229.  
  230.  cwidth = w/CW; cheight = h/CH;
  231.  
  232.  if (w*CH < h*CW)
  233.   {
  234.    x = 100;
  235.    y = (100 * h * CW) / (w * CH);
  236.    p = (w * 65536L) / CW;
  237.   }
  238.  else
  239.   {
  240.    y = 100;
  241.    x = (100 * w * CH) / (h * CW);
  242.    p = (h * 65536L) / CH;
  243.   }
  244.  
  245.  PointSize = p;
  246.  XYdpi = x<<16 | y;
  247.  
  248.  if ((last_x != x) || (last_y != y) || (last_p != PointSize))
  249.   {
  250.    FreeGlyphs();
  251.    last_x = x;
  252.    last_y = y;
  253.    last_p = PointSize;
  254.   }
  255. }
  256.  
  257. int TextLen(char *s)
  258. {
  259.  char *ch;
  260.  int   txtlen = 0;
  261.  
  262.  for(ch = s; *ch; ch++)
  263.   txtlen += (GetGlyph(*ch, FALSE))->width + 1;
  264.  
  265.  return txtlen;
  266. }
  267.  
  268. static int _STI_10000_InitGlyphs(void)
  269. {
  270.  char ch;
  271.  
  272.  for(ch=0; ch <= 255; ch++)
  273.   HGlyphs[ch] = VGlyphs[ch] = NULL;
  274.  
  275.  return 0;
  276. }
  277.  
  278. static void _STD_10000_CloseOutlineFonts(void)
  279. {
  280.  CloseOutlineFont();
  281. }
  282.